package com.java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.mapper.LendItemMapper;
import com.java.mapper.LendMapper;
import com.java.mapper.LendReturnMapper;
import com.java.pojo.entity.Lend;
import com.java.pojo.entity.LendItem;
import com.java.pojo.entity.LendItemReturn;
import com.java.mapper.LendItemReturnMapper;
import com.java.pojo.entity.LendReturn;
import com.java.service.LendItemReturnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.service.UserBindService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的出借回款记录表 服务实现类
 * </p>
 *
 * @author Helen
 * @since 2021-07-08
 */
@Service
public class LendItemReturnServiceImpl extends ServiceImpl<LendItemReturnMapper, LendItemReturn> implements LendItemReturnService {

    @Resource
    private LendItemReturnMapper lendItemReturnMapper;

    @Resource
    private LendReturnMapper lendReturnMapper;

    @Resource
    private LendMapper lendMapper;

    @Resource
    private UserBindService userBindService;

    @Resource
    private LendItemMapper lendItemMapper;

    //回款计划
    @Override
    public List<LendItemReturn> selectByLendId(Long lendId, Long investUserId) {

        QueryWrapper<LendItemReturn> wrapper = new QueryWrapper<>();
        wrapper.eq("lend_id",lendId)
                .eq("invest_user_id",investUserId)
                .orderByAsc("current_period");
        return lendItemReturnMapper.selectList(wrapper);
    }

    @Override
    public List<Map<String, Object>> getLendItemReturnDetail(Long lendReturnId) {



        //根据lendReturnId查询到回款计划
        QueryWrapper<LendItemReturn> wrapper = new QueryWrapper<>();
        wrapper.eq("lend_return_id",lendReturnId);
        List<LendItemReturn> lendItemReturnList = lendItemReturnMapper.selectList(wrapper);
        //获取还款记录
        LendReturn lendReturn = lendReturnMapper.selectById(lendReturnId);
        //获取标的信息
        Lend lend = lendMapper.selectById(lendReturn.getLendId());
        List<Map<String, Object>> list = new ArrayList<>();
        for (LendItemReturn lendItemReturn : lendItemReturnList) {
            //获取投资者对该标的出借对象
            LendItem lendItem = lendItemMapper.selectById(lendItemReturn.getLendItemId());
            Map<String, Object> map = new HashMap<>();
            //项目编号
            map.put("agentProjectCode",lend.getLendNo());
            //出借编号
            map.put("voteBillNo",lendItem.getLendItemNo());
            //投资人绑定协议编号
            String bindCode = userBindService.getBindCodeByUserId(lendItem.getInvestUserId());
            map.put("toBindCode",bindCode);
            //还款金额
            map.put("transitAmt",lendItemReturn.getTotal());
            //还款本金
            map.put("baseAmt",lendItemReturn.getPrincipal());
            //还款利息
            map.put("benifitAmt",lendItemReturn.getInterest());
            //商户手续费
            map.put("feeAmt",new BigDecimal(0));

            list.add(map);
        }
        return list;
    }

    //根据还款id返回对应的回款数据记录
    @Override
    public List<LendItemReturn> selectLendItemReturnList(Long id) {
        QueryWrapper<LendItemReturn> wrapper = new QueryWrapper<>();
        wrapper.eq("lend_return_id",id);
        return lendItemReturnMapper.selectList(wrapper);
    }
}
